package mortal.game.control
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Game.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.view.target.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class TargetController extends Controller
    {
        private var _targetModule:TargetModule;

        public function TargetController()
        {
            
        }

        override protected function initView() : IView
        {
            this._targetModule = new TargetModule();
            return this._targetModule;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.TargetGetCondition, this.onGetConditionHandler);
            Dispatcher.addEventListener(EventName.TargetGainAward, this.onGainAwardHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateComprehensionTarget, this.onReciveTargetStateHandler);
            NetDispatcher.addCmdListener(ServerCommand.GetTargetConditionSuccess, this.onGetTargetConditionSuccessHandler);
            NetDispatcher.addCmdListener(ServerCommand.GainTargetAwardSuccess, this.onGainTargetAwardSuccessHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicComprehensionCanEnd, this.onPublicComprehensionCanEndHandler);
            
        }

        private function onGetConditionHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            GameProxy.target.getTargetCondition(_loc_2);
            
        }

        private function onGainAwardHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            GameProxy.target.gainTargetAward(_loc_2);
            
        }

        private function onReciveTargetStateHandler(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SComprehensionTargetState;
            var _loc_3:* = _loc_2.state;
            if (_loc_3 != null)
            {
                TargetState.setStateArray(_loc_3);
            }
            if (TargetState.firstCanCompleteDay != 0)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.TargetEffectShow, true));
            }
            
        }

        private function onGetTargetConditionSuccessHandler(param1:Object) : void
        {
            (view as TargetModule).updateCondition(param1);
            
        }

        private function onGainTargetAwardSuccessHandler(param1:Object) : void
        {
            var _loc_2:* = param1 as int;
            TargetState.setStateByIndex((_loc_2 - 1), ETargetStateType._ETargetStateTypeCompleted);
            (view as TargetModule).updateCurrentDayState(_loc_2);
            Dispatcher.dispatchEvent(new DataEvent(EventName.TargetEffectShow, false));
            
        }

        private function onPublicComprehensionCanEndHandler(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SComprehensionCanEnd;
            var _loc_3:* = _loc_2.targetDay - 1;
            if (_loc_2.canComplete)
            {
                TargetState.setStateByIndex(_loc_3, ETargetStateType._ETargetStateTypeCanComplete);
                Dispatcher.dispatchEvent(new DataEvent(EventName.TargetEffectShow, true));
            }
            else
            {
                TargetState.setStateByIndex(_loc_3, ETargetStateType._ETargetStateTypeOnGoing);
            }
            if (this._targetModule != null)
            {
                this._targetModule.updateBtnState(_loc_2.canComplete);
            }
            
        }

    }
}
